<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <el-form-item label="模板名称" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入模板名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="模板类型" prop="templetType">
            <el-select v-model="queryParams.templetType" placeholder="请选择模板类型" clearable size="small">
              <el-option
                v-for="item in dict.print_templet_type"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="item in dict.sys_normal_disable"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['system:printTemplet:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['system:printTemplet:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['system:printTemplet:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="printTempletList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="模板名称" align="center" prop="title" width="150"/>
      <el-table-column label="模板类型" align="center" prop="templetType" width="100">
        <template slot-scope="scope">
          {{ dict.label.print_templet_type[scope.row.templetType] }}
        </template>
      </el-table-column>
      <el-table-column label="模板" align="center" width="140">
        <template slot-scope="scope">
          {{scope.row.width}}mm*{{scope.row.height}}mm
        </template>
      </el-table-column>
      <el-table-column label="纸张" align="center" width="140">
        <template slot-scope="scope">
          {{scope.row.pageWidth}}mm*{{scope.row.pageHeight}}mm
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="120" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDesign(scope.row)"
          >模板设计
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row,1)"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleCopyAdd(scope.row)"
          >复制新增
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <PrintTempletForm ref="formRef" @cancel="getList"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listPrintTemplet, delPrintTemplet, updatePrintTemplet} from "@/api/system/printTemplet";
  import PrintTempletForm from "./form.vue"
  import { ReceiptData } from './lodop/data/options'

  export default {
    name: "printTemplet",
    components: {PrintTempletForm},
    mixins: [listMixin],
    dicts: ['print_templet_type', 'sys_normal_disable',],
    data() {
      return {
        // 打印模板表格数据
        printTempletList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          templetType: null,
          width: null,
          height: null,
          pageWidth: null,
          pageHeight: null,
          tempContent: null,
          status: null,
        },
        titles:''
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询打印模板列表 */
      getList() {
        this.loading = true;
        listPrintTemplet(this.queryParams).then(response => {
          this.printTempletList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.titles = selection.map(item => item.title)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.formRef.show({"type": "ADD"});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        const option = {
          "type": "UPDATE",
          "id": id
        }
        this.$refs.formRef.show(option);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const titles = this.titles;
        this.$confirm('是否确认删除打印模板编号为"' + titles + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delPrintTemplet(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      handleStatusChange(row){
        let text = row.status === "0" ? "启用" : "停用";
        this.$confirm('确认要"' + text + '""' + row.title + '"模板吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updatePrintTemplet(row);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
      },
      handleDesign(row) {
        localStorage.setItem('tempRow',JSON.stringify(row))
        this.$router.push({
          path: '/printTemplet/lodop/designer/',
        })
      },
      handleView(row,flag) {
        let value = Object.assign(row,{tempItems:JSON.parse(row.tempContent ||'[]')})
        if ((flag = 1)) {
          this.$lodop.preview(value, ReceiptData)
        } else {
          this.$confirm('确认直接打印吗？', '确认信息')
            .then((isPass) => {
              if (isPass) {
                this.$lodop.print(value, {})
              }
            })
            .catch()
        }
      },
      handleCopyAdd(row){
        this.$refs.formRef.show({"type": "COPY",form:Object.assign(row,{id:null,title:row.title+'(复制新增)'})});
      }
    }
  };
</script>
